<template>
  <div class="login_container">
    <div class="login_box">
      <img class="logo" :src="require('../assets/logo.png')" alt />
      <!-- 表单区域 -->
      <el-form ref="form" :model="form" class="form-login" :rules="rules">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input placeholder="请输入用户名" prefix-icon="el-icon-user-solid" v-model="form.username"></el-input>
        </el-form-item>
        <!-- 密码  -->
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-s-goods"
            v-model="form.password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->

        <el-form-item style=" text-align: right;">
          <el-button type="primary" @click="submitForm('form')" size="small">登录</el-button>
          <el-button type="warning" @click="resetForm('form')" size="small">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
  
<script>
// 导入 抽离的 请求配置
import { Login_api } from "../api/index.js";
export default {
  data() {
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        //用户名表单验证
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        // 密码表单验证
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //   登录
    submitForm(form) {
      this.$refs[form].validate(async (valid) => {
        if (valid) {
          //   表单验证成功
          // 发送请求
          var res = await Login_api(this.form);
          // console.log(res,7777);s
          // 判断是否登录成功
          if (res.data.meta.status == 200) {
            this.$message({
              message: "登录成功！",
              type: "success",
            });

            // 记录token token记录在cookie中
            window.sessionStorage.setItem('token',res.data.data.token)
            // 存储用户ID
            window.sessionStorage.setItem('user',JSON.stringify(res.data.data))
            // 跳转welome页面
            this.$router.push('/welcome')
          } else {
            this.$message({
              message: "登录失败，请重新登录！",
              type: "warning",
            });
          }
        } else {
          //   表单验证失败
          
          return false;
        }
      });
    },
    //   表单重置
    resetForm(form) {
      // vue 提供的获取dom元素
      this.$refs[form].resetFields();
    },
  },
};
</script>
<style scoped lang='less'>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  position: relative;

  .login_box {
    padding: 20px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    height: 300px;
    border-radius: 3px;
    background-color: #fff;
    img.logo {
      width: 130px;
      height: 130px;
      border-radius: 50%;
      border: 10px solid #fff;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #ccc;
      box-shadow: 0 0 10px #ddd;
    }
    .form-login {
      margin-top: 80px;
    }
  }
}
</style>